<?php

namespace app\api\controller;

use app\admin\model\About;
use app\admin\model\FeedbackCate;
use app\common\controller\Api;
use think\Env;

/**
 *  意见反馈 + 帮助中心接口
 */
class Feedback extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     *  意见反馈 列表
     *
     */
    public function list()
    {
        $user_id = $this->auth->id;
        $model = new \app\admin\model\Feedback();
        $data = $model->where('user_id', $user_id)
            ->field('id,content,image,createtime')
            ->select();
        foreach ($data as &$v) {
            $v['image'] = Env::get('app.yuming') . $v['image'];

            $v['createtime'] = date('Y-m-d H:i', $v['createtime']);
        }
        $this->success('获取成功', $data, 1);

    }

    /**
     * 问题反馈内容
     *
     */

    public function index()
    {
        // 反馈内容类型
        $model = new FeedbackCate();
        $cate = $model->where(['status' => 1])->field('id,title')->select();

        // 添加问题反馈内容
        $model = new \app\admin\model\Feedback();
        $data = input();
        if (empty($data)) {
            $this->error('请传入参数', [], 0);
        }
        $cate['id'] = $data['feedback_cate_id'];
        $data['user_id'] = $this->auth->id;
        // 上传文件
        if ($files = $_FILES) {
            $imageArr = Array();
            foreach ($files as $file) {
                $imageName = $file['name'];
                //后缀名
                $ext = strtolower(substr(strrchr($imageName, '.'), 1));
                //保存文件名
                $fileName = uniqid();
                $tmp = $file['tmp_name'];
                //保存 = 路径 + 文件名 + 后缀名
                $imageSavePath = '../public/uploads/images/' . $fileName . '.' . $ext;
                $info = move_uploaded_file($tmp, $imageSavePath);

                if ($info) {
                    $path = "/uploads/images/" . $fileName . '.' . $ext;
                    array_push($imageArr, $path);
                } else {
                    $this->error(400, '上传失败！');
                }
            }
            $data['image'] = implode(',', $imageArr);
        }

        $res = $this->validate($data, 'Feedback.add');
        if ($res !== true) $this->error($res, [], 0);
        if ($model->save($data)) {
            $this->success('添加成功！', [], 1);
        }
        $this->error('添加失败！', [], 0);
    }


    /**
     *
     * 帮助中心
     */
    public function help()
    {
        // 常见问题列表
        $model = new About();
        $list = $model->where(['status' => 1, 'type' => 5])
            ->field('id,title,image,createtime')
            ->select();
        foreach ($list as &$v) {
            $v['image'] = Env::get('app.yuming') . $v['image'];
            $v['createtime'] = date('Y-m-d H:i', $v['createtime']);
        }

        // 详情页面
        $list_id = input('id/d');
        $detail = About::where(['status' => 1, 'id' => $list_id])
            ->field('id,title,content,image')
            ->find();
        $detail['image'] = Env::get('app.yuming') . $v['image'];
        $this->success('获取成功', $data = ['list' => $list, 'detail' => $detail], 1);
    }
}
